package uebungsblatt09.aufgabe15a;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Scanner;

public class Main
{
	private Model model;
	private static final String XML_FILENAME = "ModelViewController.xml";

	public Main()
	{
		model = new Model();
	}

	private void start()
	{
		int eingabe = -1;
		while (eingabe != 0)
		{
			System.out.println("Was wollen Sie?\n"
					+ " (0) Programm beenden\n"
					+ " (1) neuen View erstellen\n"
					+ " (2) neuen Controller erstellen\n"
					+ " (3) Controller zum Aendern auswaehlen\n"
					+ " (4) XML-Daten laden\n");
			eingabe = new Scanner(System.in).nextInt();
			switch (eingabe)
			{
			case 1:
				newView();
				break;
			case 2:
				newController();
				break;
			case 3:
				chooseController();
				break;
			case 4:
				loadXML();
				break;
			}
		}
	}
	
	private void loadXML()
	{
    model = readModel();
    showLoadedData();
	}

	private void showLoadedData()
  {
    System.out.println("Folgende Daten wurden geladen: ");
    System.out.println("-----------------------------");
    System.out.println();
    System.out.println("Model: ");
    System.out.println(" W: "+model.getW());
    System.out.println();
    
    System.out.println("Controllers: ");
    for(Controller c : model.getControllers())
    {
      System.out.println(" Command: "+c.getCommand());
      System.out.println(" Model: "+c.getModel());
      model = c.getModel();
    }
    System.out.println();
    
    System.out.println("Views:");
    for(ModelListener ml : model.getModelListeners())
      System.out.println(((View)ml).getZ());
    System.out.println();
  }

  private void writeModel(Model model)
	{
		XMLEncoder e;
		try
		{
			e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(
					XML_FILENAME)));
			
  		e.writeObject(model);
			e.close();
		}
		catch (FileNotFoundException e1)
		{
			e1.printStackTrace();
		}
	}
	
	private Model readModel()
  {
    XMLDecoder d = null;
    Model model = null;
    try
    {
      d = new XMLDecoder(new BufferedInputStream(new FileInputStream(XML_FILENAME)));
      model = (Model)d.readObject();
      d.close();
    }
    catch (FileNotFoundException e)
    {
      e.printStackTrace();
    }
    return model;
  }
	
	private void newView()
	{
		System.out.print("Welches Ausgabezeichen? ");
		String zeichen = new Scanner(System.in).nextLine();

		model.addModelListener(new View(model, zeichen));
    
    writeModel(model);
	}

	private void newController()
	{
		System.out.print("Welche Eingabeaufforderung? ");
		String command = new Scanner(System.in).nextLine();

		Controller controller = new Controller(model, command);
		model.addController(controller);
    
    writeModel(model);
	}

	private void chooseController()
	{
		System.out.println("Welchen der Controller? ");
		for (int i = 0; i < model.getControllers().size(); i++)
			System.out.println(" (" + (i + 1) + ") "
					+ model.getControllers().get(i).getCommand());
		int auswahl = new Scanner(System.in).nextInt();

		System.out.print(model.getControllers().get(auswahl - 1).getCommand()
				+ ": ");
		model.getControllers().get(auswahl - 1)
				.changeModel(new Scanner(System.in).nextInt());
	}

	public static void main(String[] args)
	{
		new Main().start();
	}
}
